#pragma once 

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>

using std::string;

class Util{
  public:
    static int Readline(int sock,std::string &out)
    {
      char ch='X';
      while(ch!='\n')
      {
        ssize_t s=recv(sock,&ch,1,0);
        if(s>0)
        {
          if(ch=='\r')
          {
            recv(sock,&ch,1,MSG_PEEK);//窥探下一个字符但不取走
            if(ch=='\n')
            {
              //转换\r\n->\n
              //窥探成功读取下一个字符
              recv(sock,&ch,1,0);
            }
            else
            {
                ch='\n';
            }
          }
          out+=ch;
        }
        else if(s==0)
        {
          //对端关闭
          return 0;
        }
        else{
          //对端出错
          return -1;
        }
      }//while
      return out.size();
    }

    static bool Cutstring(string& line ,string& sub1_out,string& sub2_out,string sep)
    {
      size_t pos=line.find(sep);
      if(pos!=string::npos)
      {
        sub1_out=line.substr(0,pos);
        sub2_out=line.substr(pos+sep.size());
        return true;
      }
      return false;
    }

};
